package pe.edu.upc.dew.grupo04.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.edu.upc.dew.grupo04.model.Pelicula;
import pe.edu.upc.dew.grupo04.service.FuncionService;
import pe.edu.upc.dew.grupo04.service.PeliculaService;
import pe.edu.upc.dew.grupo04.service.SalaService;

public class InicioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private PeliculaService peliculaService;
	
	public void init(ServletConfig config) throws ServletException {
		
		this.peliculaService = WebApplicationContextUtils
		.getWebApplicationContext(config.getServletContext()).getBean(
				"peliculaService", PeliculaService.class);
		
	}
	
	
    public InicioServlet() {
        super();
        
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {		
			List<Pelicula> peliculas = peliculaService.getPeliculas();
			request.setAttribute("peliculas", peliculas);
			request.getRequestDispatcher("index.jsp").forward(request,response);
		}
		catch (Exception e) {
			String Cadena = " No se pudo mostrar la cartelera, por favor verificar..." + e.getMessage();			
			request.setAttribute("Cadena", Cadena);			
			request.getRequestDispatcher("error.jsp").forward(request,response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*
		try {		
				List<Pelicula> peliculas = peliculaService.getPeliculas();
				request.setAttribute("peliculas", peliculas);
				request.getRequestDispatcher("index.jsp").forward(request,response);
		}
		catch (Exception e) {
			String Cadena = " No se pudo mostrar la cartelera, por favro verificar..." ;			
			request.setAttribute("Cadena", Cadena);			
			request.getRequestDispatcher("error.jsp").forward(request,response);
		}
		*/
	}

}
